﻿/*
         /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">来自form表单的文件信息</param>
        /// <returns></returns>
        [HttpPost, Route("Upload")]
        public ActionResult Upload([FromForm] IFormFile file)
        {
            return Ok(new {newFileName=ImgHelper.Upload(file) });
        }
*/

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;

namespace BWShop.Common
{
    public class ImgHelper
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">来自form表单的文件信息</param>
        /// <returns></returns>
        public static string Upload(IFormFile file)
        {
            string rootdir = AppContext.BaseDirectory.Split(@"\bin\")[0];
            string fname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + System.IO.Path.GetExtension(file.FileName);
            var path = rootdir + @"\Upload\" + fname;
            using (System.IO.FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Flush();//清空文件流
            }
            return "http://localhost:8081/" + fname ;//将能访问新文件的网址回传给前端


        }
    }
}
